package 面试鸭;

/**
 * @author tmh
 * @date 2024/9/15 21:19
 * @description
 */
public class T大数相加 {
    public static void main(String[] args) {
        String sum = addSum("123", "456");
        System.out.println(sum);

    }

    public static String addSum(String s1, String s2) {
        //carry表示进位
        int carry = 0;
        int i = s1.length() - 1;
        int j = s2.length() - 1;
        StringBuilder result = new StringBuilder();
        while (i >= 0 || j >= 0 || carry != 0) {
            int num1 = s1.charAt(i) - '0';
            int num2 = s2.charAt(j) - '0';
            int sum = num1 + num2 + carry;
            result.append(sum % 10);
            carry = sum / 10;
            i--;
            j--;
        }
        return result.reverse().toString();
    }
}
